<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/2/23
 * Time: 12:14
 */
namespace Api\Controller;
use Think\Controller;
class MessController extends Controller {

    //获取消息
     public function getAllMessage(){
         //解决跨域
         echoHeader();
         $type=I('post.type',0);       //参数0获取全部，1获取未读，2获取已读
         $username=I('post.user_id',null);
         $method=I('post.method',1,'int');     //1业主 2师傅

         if(!$username){
             $data=array(errcode=>1001,errmsg=>'缺少用户id');
             $this->ajaxReturn($data,'JSON');
         }


         //总消息数
         $allWhere['n.status']=array('eq',1);
         $allWhere['m.user_id']=$username;
         $allWhere['m.is_del']=0;
         $allData=M('messuser m')->join('left join zsf_notice n on m.message_id=n.message_id')->where($allWhere)->count();
         //未读消息数
         $unwhere['n.status']=array('eq',1);
         $unwhere['m.is_read']=array('eq',1);
         $unwhere['m.user_id']=$username;
         $unwhere['m.is_del']=0;
         $unData=M('messuser m')->join('left join zsf_notice n on m.message_id=n.message_id')->where($unwhere)->count();


         if($type==1){
             $where['m.is_read']=array('eq',1);
         }elseif($type==2){
             $where['m.is_read']=array('eq',2);
         }else{
             $where['m.is_read']=array('neq',3);
         }

           $where['n.status']=array('eq',1);
          // $where['n.type']=array('eq',$method);
           $where['m.user_id']=$username;
           $where['m.is_del']=0;
           $data=M('messuser m')->join('left join zsf_notice n on m.message_id=n.message_id')->where($where)->select();

           if(!empty($data)){
               foreach($data as $k=>$v){
                   $data[$k]['create_time']=workTime($v['create_time']);
               }
           }


           $sum['Count']=(int)$allData;       //总消息数
           $sum['unreadCount']=(int)$unData;     //未读消息数
           $sum['readCount']=$sum['Count']-$sum['unreadCount'];    //已读消息数
           $dat=array(errcode=>200,errmsg=>$data,num=>$sum);
           //更改未读为已读
          if($type==1){
              $unwhe['is_read']=1;
              $unwhe['user_id']=$username;
              $unsa['is_read']=2;
              M('messuser')->where($unwhe)->save($unsa);
          }
           $this->ajaxReturn($dat,'JSON');



     }

    //更改消息状态
    public function updateMess(){
        //解决跨域
        echoHeader();
        $messageid=I('post.messageid');
        $user_id=I('post.user_id');
        if(empty($messageid) || empty($user_id)){
            $data=array(errcode=>1001,errmsg=>'用户或消息不能为空');
            $this->ajaxReturn($data,'JSON');
        }
        $sqldel=implode(',',$messageid);
        $message=M('messuser');
        $message->is_read=3;
        $where="user_id='$user_id' and id in($sqldel)";
        $resource=$message->where($where)->save();
        if($resource===false){
            $data=array(errcode=>403,errmsg=>'删除失败');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array(errcode=>200,errmsg=>'删除成功');
            $this->ajaxReturn($data,'JSON');
        }
    }

    //删除消息
    public function delMessage(){
        //解决跨域
        echoHeader();
        $messageid=I('post.messageid');
        $user_id=I('post.user_id');

        if(empty($messageid) || empty($user_id)){
            $data=array(errcode=>1001,errmsg=>'用户或消息不能为空');
            $this->ajaxReturn($data,'JSON');
        }

        $sqldel=implode(',',$messageid);
        $message=M('messuser');
        $mess['is_del']=1;
        $where['user_id']=$user_id;
        $where['message_id']=array('in',"$sqldel");
        $resource=$message->where($where)->save($mess);

        if($resource===false){
            $data=array(errcode=>403,errmsg=>'删除失败');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array(errcode=>200,errmsg=>'删除成功');
            $this->ajaxReturn($data,'JSON');
        }
    }
}

